/**
 * 队伍  rmnet队伍的对象类
 * @class
 */
class Net_Party {
    constructor() {
        this._leader = null;
        this._member = [];
        this._data = new Map();
    }

    setup(data) {
        const { leader, member } = data;
        this._leader = leader;
        this._member = member;
        const arr = [];
        if (!this.setMember(leader)) arr.push(leader);
        for (const uid of member) {
            if (!this.setMember(uid)) arr.push(uid);
        }
        if (arr.length > 0) {
            //获取队伍成员信息
            NetManager.getPtPlayerBaseInfo(arr);
        } else {
            const window = SceneManager._scene?._partyWindow;
            if (window) {
                window.refresh();
            }
        }
    }

    setMember(uid) {
        if (this._data.has(uid) || uid === NetManager.actor.uid) return true;
        const player = NetManager.players.get(uid);
        if (player) {
            this._data.set(uid, player.getData());
            return true;
        }
        return false;
    }

    leader() {
        return this._leader;
    }

    member() {
        return this._member;
    }

    data() {
        return this._data;
    }

    size() {
        return this._data.size;
    }

    join(uid) {
        this._member.push(uid);
        const data = NetManager.players.get(uid);
        if (data) {
            this._data.set(uid, data);
        }
    }

    leave(uid) {
        this._member.remove(uid);
        this._data.delete(uid);
    }

    include(data) {
        for (const member of data) {
            const { uid } = member;
            if (this._member.includes(uid) && !this._data.has(uid)) {
                this._data.set(uid, member);
            }
        }
        //队伍窗口刷新
        const window = SceneManager._scene?._partyWindow;
        if (window) {
            window.refresh();
        }
    }
}